//在各个楼盘中有哪些公司
package main

import (
	"bytes"
	"flag"
	"fmt"
	goq "lib/goquery"
	"lib/request"
	"os"
	"runtime"
	"strings"
	"sync"
	"time"
)

var (
	main_domain = "" //主域名
	LOG         = ""
	NumCPU      = runtime.NumCPU()
	waitgroup   = &sync.WaitGroup{}
)

func init() {
	runtime.GOMAXPROCS(NumCPU)
	city_en := flag.String("city_en", "bj", "")
	flag.Parse()
	main_domain = "http://" + *city_en + ".anjuke.com"
	LOG = *city_en + ".anjuke"
}

func main() {
	getDistrict("/tycoon/st1")

	s(time.Now().Format("2006-01-02 15:04:05"), "done.")
}

//读取区域列表
func getDistrict(url string) {
	tmpurl := fmt.Sprintf("%s%s", main_domain, url)
	doc, e := getDoc(tmpurl)
	if e != nil {
		errlog(fmt.Sprintf("读取区域列表失败[%s]:[%s]", tmpurl, e.Error()))
		return
	}
	doc.Find(".items .elems-l a").Each(func(i int, sec *goq.Selection) {
		var (
			district, url string
		)
		url, ok := sec.Attr("href")
		if ok {
			district = filterStr(sec.Text())
			getBlock(url, district)
		}
	})
}

//读取热点
func getBlock(tmpurl, district string) {
	doc, e := getDoc(tmpurl)
	if e != nil {
		errlog(fmt.Sprintf("读取热点列表失败[%s]:[%s]", tmpurl, e.Error()))
		return
	}
	doc.Find(".sub-items a").Each(func(i int, sec *goq.Selection) {
		var (
			block, url string
		)
		url, ok := sec.Attr("href")
		if ok {
			block = filterStr(sec.Text())
			getAgentListPage(url, district, block)
		}
	})
}

//读取经纪人列表分页
func getAgentListPage(tmpurl, district, block string) {
	tmpurl = strings.Replace(tmpurl, "st1", "", 1)
	var curpage = 1
	for {
		homeurl := fmt.Sprintf("%sp%d/", tmpurl, curpage) //首页

		doc, e := getDoc(homeurl)
		if e != nil {
			errlog(fmt.Sprintf("读取经纪人列表分页[%s]:[%s]", tmpurl, e.Error()))
			continue
		}
		//是否有内容
		if doc.Find(".list-content > .jjr-itemmod").Length() == 0 {
			break
		}

		getAgentList(doc, block, district)

		curpage++
		time.Sleep(time.Second * 1)
	}
}

//读取经纪人列表
func getAgentList(doc *goq.Document, block, district string) {
	doc.Find(".list-content > .jjr-itemmod").Each(func(i int, sec *goq.Selection) {
		var (
			name          string //姓名
			mobile_number string //手机号码
			job           string //公司门店
			communityname string //熟悉楼盘
		)
		name = filterStr(sec.Find(".jjr-info h3 a").Text())
		mobile_number = filterStr(sec.Find(".jjr-info .tel").Text())
		tmp_job_arr := sec.Find(".jjr-info address a")
		if tmp_job_arr.Length() == 2 {
			job = tmp_job_arr.Eq(0).Text() + "#" + tmp_job_arr.Eq(1).Text()
		}
		communityname = filterStr(sec.Find(".jjr-info .xq_tag:contains('熟悉')").Text())
		if "" == communityname {
			return
		}
		communityname = strings.Replace(communityname, "熟悉", "", -1)
		communityname = string(bytes.Replace([]byte(communityname), []byte{0xc2, 0xa0, 0xc2, 0xa0, 0xc2, 0xa0}, []byte{','}, -1))
		str := district
		str += "#" + block
		str += "#" + name
		str += "#" + mobile_number
		str += "#" + job
		str += "#" + communityname

		outlog(str)
	})
}

//读取doc
func getDoc(url string) (*goq.Document, error) {
	res, e := request.Get(url)
	if e != nil {
		return nil, e
	}
	doc, e := goq.NewDocumentFromResponse(res)
	if e != nil {
		return nil, e
	}
	return doc, nil
}

func filterStr(str string) string {
	str = strings.Replace(str, "\r\n", "", -1)
	str = strings.Replace(str, "\r", "", -1)
	str = strings.Replace(str, "\n", "", -1)
	str = strings.Replace(str, "#", "", -1)
	str = strings.TrimSpace(str)
	return str
}

func s(a ...interface{}) {
	fmt.Println(a)
}

//outlog
func outlog(str string) {
	pwd, _ := os.Getwd()
	outlog := fmt.Sprintf("%s/%s_log.txt", pwd, LOG)
	file, err := os.OpenFile(outlog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
	defer file.Close()
	if err != nil {
		return
	}
	str += "\n"
	file.WriteString(str)
}

//errlog
func errlog(str string) {
	pwd, _ := os.Getwd()
	outlog := fmt.Sprintf("%s/%s_err.txt", pwd, LOG)
	file, err := os.OpenFile(outlog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
	defer file.Close()
	if err != nil {
		return
	}
	str += "\n"
	file.WriteString(str)
}

//debuglog
func debuglog(str string) {
	pwd, _ := os.Getwd()
	outlog := fmt.Sprintf("%s/%s_debug.txt", pwd, LOG)
	file, err := os.OpenFile(outlog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
	defer file.Close()
	if err != nil {
		return
	}
	str += "\n"
	file.WriteString(str)
}
